<template>
  <el-card style="margin: 20px;border-radius: 10px;">
    <el-form ref="form" :label-position="labelPosition" label-width="80px" :model="formData" :rules="rules">
      <el-row>
        <el-col :span="6" class="padsize">
          <el-form-item label="库区编号" prop="code">
            <el-input
              v-model="formData.code"
              disabled
            /></el-form-item>
        </el-col>
        <el-col :span="6" class="padsize">
          <el-form-item label="所属仓库" prop="warehouseId">
            <el-select
              v-model="formData.warehouseId"
              placeholder="请选择"
              clearable
              style="width: 100%;"
            >
              <el-option
                v-for="item in warehouseOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="padsize">
          <el-form-item label="库区名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6" class="padsize">
          <el-form-item label="温度类型" prop="temperatureType">
            <el-select
              v-model="formData.temperatureType"
              placeholder="请选择"
              clearable
              style="width: 100%;"
            >
              <el-option label="常温" value="CW" />
              <el-option label="冷藏" value="LC" />
              <el-option label="恒温" value="HW" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6" class="padsize">
          <el-form-item label="承重类型" prop="bearingType">
            <el-select
              v-model="formData.bearingType"
              placeholder="请选择"
              clearable
              style="width: 100%;"
            >
              <el-option label="重型" value="ZX" />
              <el-option label="轻型" value="QX" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="padsize">
          <el-form-item label="用途属性" prop="useType">
            <el-select
              v-model="formData.useType"
              placeholder="请选择"
              style="width: 100%;"
              clearable
            >
              <el-option label="入库缓存区" value="RKHCQ" />
              <el-option label="出库缓存区" value="CKHCQ" />
              <el-option label="存储区" value="CCQ" />
              <el-option label="分拣区" value="FJQ" />
              <el-option label="质检区" value="ZJQ" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="padsize">
          <el-form-item label="负责人" prop="personName">
            <el-input v-model="formData.personName" placeholder="请输入" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6" class="padsize">
          <el-form-item label="联系电话" prop="phone">
            <el-input
              v-model="formData.phone"
              placeholder="请输入"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6" class="padsize">
          <el-form-item label="库区状态" prop="status">

            <el-radio-group v-model="formData.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <div class="btnflexbox">
        <el-button round class="back" @click="$router.back()">返回</el-button>
        <el-button
          type="success"
          round
          class="submit"
          @click="submitBtn"
        >提交</el-button>
      </div>
    </el-row>
  </el-card>
</template>

<script>
import { setAreaList, getCode, getwarehouseList
  , bckAreaList, updataAreaList } from '@/api/area'

export default {
  data() {
    return {
      labelPosition: 'top',
      areaId: this.$route.params.id,
      formData: {
        code: '',
        warehouseId: '',
        name: '',
        temperatureType: '',
        bearingType: '',
        useType: '',
        personName: '',
        phone: '',
        status: ''
      },
      warehouseOptions: [],
      rules: {
        code: [
          { required: true }
        ],
        warehouseName: [
          { required: true, message: '请选择所属仓库', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入库区名称', trigger: 'blur' }
        ],
        temperatureType: [
          { required: true, message: '请选择温度类型', trigger: 'blur' }
        ],
        bearingType: [
          { required: true, message: '请选择承重类型', trigger: 'blur' }
        ],
        useType: [
          { required: true, message: '请选择用途属性', trigger: 'blur' }
        ],
        personName: [
          { required: true, message: '请输入负责人', trigger: 'blur' }
        ],
        phone: [
          { message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        radio: [
          { required: true }
        ]
      }
    }
  },
  async created() {
    if (this.$route.params.id === 'null') {
      const res = await getCode()
      this.formData.code = res.data.data
      console.log('新增')
    } else {
      this.getAreaBck()
    }
    const res2 = await getwarehouseList(this.formData.status)
    this.warehouseOptions = (res2.data.data).map(item => {
      return {
        label: item.name,
        value: item.id
      }
    })
  },
  methods: {
    submitBtn() {
      if (this.areaId) {
        this.saveArea()
      } else {
        this.setArea()
      }
    },
    async setArea() {
      // 校验表单
      await this.$refs.form.validate()
      // console.log(this.formData)
      this.formData.status = +this.formData.status
      const res = await setAreaList(this.formData)
      console.log(res)
      this.$message.success('新增成功！')
      this.$router.push('/info/area/')
    },
    // 保存修改
    async saveArea() {
      const res = await updataAreaList(this.formData)
      console.log('updata', res)
      this.formData = res.data.data
      this.$message.success('修改成功！')
      this.$router.push('/info/area/')
    },
    // 数据回显
    async getAreaBck() {
      console.log(this.areaId)
      const res = await bckAreaList(this.areaId)
      this.formData = res.data.data
      console.log(this.formData)
    }

  }
}

</script>

<style scoped lang="scss">
*{
  margin: 0;
  padding: 0;
}
.padsize{
  padding-right:20px;
  ::v-deep .el-input{
    padding-top:0;

    }
  }
  ::v-deep .el-form-item__label{
      padding: 0;
      font-size: 14px;
      font-weight: 100;
    }
 .btnflexbox{
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  margin-top:20px;
  .back{
    margin-right:20px ;
    padding: 10px 35px 10px 35px;
  }
  .submit{
    margin-left:20px ;
    padding: 10px 35px 10px 35px;

  }
 }
</style>
